package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RAccountBean;
import com.xoms.admin.controller.res.bean.RAccountLoginBean;
import com.xoms.admin.controller.res.bean.RAccountRolesBean;
import com.xoms.admin.controller.res.bean.RHaveResourcesBean;
import com.xoms.admin.controller.res.bean.RVertifyVCodeBean;
import com.xoms.admin.controller.springmvc.req.AddAccountRolesParam;
import com.xoms.admin.controller.springmvc.req.CheckTokenParam;
import com.xoms.admin.controller.springmvc.req.DelAccountParam;
import com.xoms.admin.controller.springmvc.req.DelAccountRolesParam;
import com.xoms.admin.controller.springmvc.req.DisableAccountParam;
import com.xoms.admin.controller.springmvc.req.EditAccountParam;
import com.xoms.admin.controller.springmvc.req.EditPwdParam;
import com.xoms.admin.controller.springmvc.req.EnableAccountParam;
import com.xoms.admin.controller.springmvc.req.FindPasswordParam;
import com.xoms.admin.controller.springmvc.req.GetAccountResourcesParam;
import com.xoms.admin.controller.springmvc.req.GetAccountRolesParam;
import com.xoms.admin.controller.springmvc.req.GetAccountsParam;
import com.xoms.admin.controller.springmvc.req.LoginParam;
import com.xoms.admin.controller.springmvc.req.SendVCodeParam;
import com.xoms.admin.controller.springmvc.req.UpdatePasswordParam;
import com.xoms.admin.controller.springmvc.req.VertifyVCodeParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.AccountService;
import com.xoms.admin.service.ResourceService;
import com.xoms.admin.service.RoleService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;
import com.xoms.util.StringUtil;

@Component
public class AccountController {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	AccountService accountServiceImpl;
	
	@Autowired
	RoleService roleServiceImpl;
	
	@Autowired
	ResourceService resourcesServiceImpl;

	/**
	 * 验证用户token
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAccountLoginBean> checkToken(CheckTokenParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> result = new ObjectResult<>();
		try {
			// 获取传递的token
			String token = StringUtil.isEmpty(param.getToken()) ? "" : param.getToken();
			// 根据token获取账号信息
			RAccountLoginBean data = accountServiceImpl.getAccountByToken(token);
			result.setData(data);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 账号登录
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAccountLoginBean> login(LoginParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> result = new ObjectResult<>();
		try {
			// 获取手机号和密码
			String phoneNum = param.getPhone();
			String password = param.getPassword();
			// 验证登录并返回用户信息
			RAccountLoginBean data = accountServiceImpl.login(phoneNum, password);
			result.setData(data);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 发送验证码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult sendVCode(SendVCodeParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			int type = NumUtil.toInt(param.getType(), 1);//type:1 找回密码
			// 根据type发送手机验证码
			boolean flag = accountServiceImpl.sendVCode(type, param.getPhone());
			result.setFlag(flag);
		}  catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			logger.error(e.getMessage(), e);
		}
		return result;
	}
	
	/**
	 * 验证验证码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RVertifyVCodeBean> vertifyVCode(VertifyVCodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RVertifyVCodeBean> result = new ObjectResult<RVertifyVCodeBean>();
		try {
			int type = NumUtil.toInt(param.getType(), 1);// type：1，验证找回密码验证码
			// 验证成功返回临时token，根据token更新密码
			RVertifyVCodeBean data = accountServiceImpl.vertifyVCode(type, param.getPhone(), param.getVcode());
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 忘记密码，验证手机后，更新密码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult findPassword(FindPasswordParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			// 找回密码时验证手机验证码后的临时token
			String tempToken = param.getTempToken();
			// 新密码
			String pwd = param.getPwd();
			String submitPwd = param.getSubmitPwd();

			boolean flag = accountServiceImpl.findPassword(tempToken, pwd, submitPwd);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 更新密码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult updatePassword(UpdatePasswordParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			// 账号个人更新密码：输入旧密码和新密码
			String oldPwd = param.getPassword();
			String newPwd = param.getNewPassword();
			String submitPwd = param.getSubmitPassword();
			// 返回更新是否成功
			boolean flag = accountServiceImpl.updatePassword(account, oldPwd, newPwd, submitPwd);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取账号列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RAccountBean> getAccounts(GetAccountsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RAccountBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			int deptId = NumUtil.toInt(param.getDeptId(), 0);
			int roleId = NumUtil.toInt(param.getRoleId(), 0);
			
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取账号列表
			PageResult<RAccountBean> resData = accountServiceImpl.getAccounts(account, roleId, deptId, key, page, pagesize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	
	/**
	 * 编辑账号
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAccountBean> edit(EditAccountParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RAccountBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			String accid = param.getAccid();// 不为空则是编辑账号
			String icon = param.getIcon();// 头像
			String name = param.getName();// 姓名
			String phoneNum = param.getPhoneNum();// 手机号
			int deptId = NumUtil.toInt(param.getDeptId(), 0);// 部门id,0不存
			
			//成功后返回编辑后账号
			RAccountBean data = accountServiceImpl.edit(account, accid, icon, name, phoneNum, deptId);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 管理员重置密码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult editPwd(EditPwdParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			// 重置密码账号id
			String accid = param.getAccid();
			// 新密码,输入空则设置为000000
			String password = StringUtil.isEmpty(param.getPassword()) ? "000000" : param.getPassword();
			
			boolean flag = accountServiceImpl.editPwd(account, accid, password);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 启用账号
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult enable(EnableAccountParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 要启用账号id

			boolean flag = accountServiceImpl.enable(account, accid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 禁用账号
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult disable(DisableAccountParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 要禁用账号id

			boolean flag = accountServiceImpl.disable(account, accid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取账号角色列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RAccountRolesBean> getAccountRoles(GetAccountRolesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RAccountRolesBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 账号id
			
			// 获取账号角色列表
			PageResult<RAccountRolesBean> resData = roleServiceImpl.getAccountRoles(account, accid);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取账号资源列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RHaveResourcesBean> getAccountResources(GetAccountResourcesParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ListResult<RHaveResourcesBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 账号id
			
			// 获取账号权限列表
			PageResult<RHaveResourcesBean> resData = resourcesServiceImpl.getAccountResources(account, accid);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除账号信息
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult del(DelAccountParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 要删除的账号id

			boolean flag = accountServiceImpl.del(account, accid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 添加账号角色
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult addAcountRoles(AddAccountRolesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 要编辑的账号id
			String roleIds = param.getRids();// 获取权限id数组

			boolean flag = roleServiceImpl.addAcountRoles(account, accid, roleIds);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删账号角色
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delAcountRoles(DelAccountRolesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String accid = param.getAccid();// 要账号id
			String roleIds = param.getRids();// 获取删除权限id数组

			boolean flag = roleServiceImpl.delAcountRoles(account, accid, roleIds);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
